<%@page language="java" session="true" 
import="java.io.*,java.util.*,java.io.*,java.sql.*,javax.servlet.*"%> 

	
<%
	//to get the content type information from JSP Request Header
	String DNoteNameVar=request.getParameter("DNoteName");
	String contentType = request.getContentType();
	//here we are checking the content type is not equal to Null and as well as the passed data from mulitpart/form-data is greater than or equal to 0
	if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
 		DataInputStream in = new DataInputStream(request.getInputStream());
		//we are taking the length of Content type data
		int formDataLength = request.getContentLength();
		byte dataBytes[] = new byte[formDataLength];
		int byteRead = 0;
		int totalBytesRead = 0;
		//this loop converting the uploaded file into byte code
		while (totalBytesRead < formDataLength) {
			byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
			totalBytesRead += byteRead;
			}

		String file = new String(dataBytes);
		//for saving the file name
		String saveFile = file.substring(file.indexOf("filename=\"") + 10);
		saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
		saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
		int lastIndex = contentType.lastIndexOf("=");
		String boundary = contentType.substring(lastIndex + 1,contentType.length());
		int pos;
		//extracting the index of file 
		pos = file.indexOf("filename=\"");
		pos = file.indexOf("\n", pos) + 1;
		pos = file.indexOf("\n", pos) + 1;
		pos = file.indexOf("\n", pos) + 1;
		int boundaryLocation = file.indexOf(boundary, pos) - 4;
		int startPos = ((file.substring(0, pos)).getBytes()).length;
		int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
		String root =  getServletConfig().getServletContext().getRealPath("/")+File.separator+"adobefile/QualityManual/DeviationNotes/";   
		String root2="adobefile/QualityManual/DeviationNotes/"; 				                  
		String LoadInto=root+saveFile;
		// creating a new file with the same name and writing the content in new file
		FileOutputStream fileOut = new FileOutputStream(LoadInto);
		fileOut.write(dataBytes, startPos, (endPos - startPos));
		fileOut.flush();
		fileOut.close();

   		
	
		

		Connection con=null,con1=null; 
		PreparedStatement stmt=null,stmt1=null; 
		PreparedStatement ps=null,ps1=null; 
		ResultSet rs=null,rs1=null; 
		String sql="",sql1="",a=""; 
		Class.forName("com.mysql.jdbc.Driver"); 
		String connectionURL = "jdbc:mysql://localhost:3306/qmsdb"; 	
		con=DriverManager.getConnection(connectionURL,"root","password"); 
		int j1=0; 

		try 
		{ 
			sql="INSERT INTO deviationnotes (DNoteName, DNoteURL) Values (?, ?)"; 
			stmt = con.prepareStatement(sql);
			stmt.setString(1, DNoteNameVar);
			stmt.setString(2, root2+saveFile);
			j1=stmt.executeUpdate();
		if(j1!=0) 
		{ 
		%> 
		<script type="text/javascript"> 
	
			alert('File Successfully Uploaded!\nYou will be directed back to the previous page once you click "OK"'); 
			window.location="QM_NewDeviationNotes.jsp";
		</script> 
		<% 

		} 

		} 
		catch(Exception ex) 
		{ 
			ex.printStackTrace(); 
		} 

		} 
		%>	
	

